import type { GoogleElements } from '../../../../../../types/constants/personalization/google'
import type { ThemeType } from '../../../../../../types/interfaces'
import { SearchThemeService } from '../../../../../../lib/abstract'
import { PageHandler } from '../../../../../../lib/page_handler'
import { GooglePageType } from '../../../../../../lib/page_handler/platform/google/types'
import {
  UNDEFINED_PAGE_TYPE,
  UNDEFINED_PAGE_TYPE_TIPS,
} from '../../../../../../lib/page_handler/type'
import {
  BODY_SELECTOR,
  logger,
  loggerDeduplicator,
  PLATFORM_PAGE_TYPES,
  SUPPORTED_PLATFORMS,
} from '../../../../../../types/constants'
import { GoogleIndexPage } from './google/page'
import { GoogleNotFoundPage } from './google/page/404'
import { GoogleAdvancedPage } from './google/page/advanced'
import { GoogleAllPage } from './google/page/all'
import { GoogleBooksPage } from './google/page/books'
import { GoogleCommonPage } from './google/page/common'
import { GoogleIndexHkPage } from './google/page/index_hk'
import { GoogleNewsPage } from './google/page/news'
import { GoogleShortVideoPage } from './google/page/short_video'
import { GoogleVideoPage } from './google/page/video'
import { GoogleWebPage } from './google/page/web'

/**
 * 页面处理器
 */
const PAGE_HANDLERS: Partial<Record<GooglePageType, any>> = {
  [GooglePageType.Index]: GoogleIndexPage,
  [GooglePageType.IndexHk]: GoogleIndexHkPage,
  [GooglePageType.All]: GoogleAllPage,
  [GooglePageType.Video]: GoogleVideoPage,
  [GooglePageType.News]: GoogleNewsPage,
  [GooglePageType.ShortVideo]: GoogleShortVideoPage,
  [GooglePageType.Web]: GoogleWebPage,
  [GooglePageType.Books]: GoogleBooksPage,
  [GooglePageType.Advanced]: GoogleAdvancedPage,
  [GooglePageType.NotFound]: GoogleNotFoundPage,
}

/**
 * 主题服务测试链接
 */
export class GoogleThemeService extends SearchThemeService {
  readonly name: string = 'GoogleTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.SEARCH.value
  private static instance: GoogleThemeService
  declare readonly elements: GoogleElements
  private readonly commonPage: GoogleCommonPage = GoogleCommonPage.getInstance()
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private currentPageType: GooglePageType | 'default' = this.pageHandler.pages.google.pageType
  readonly wrapperSelector: string = BODY_SELECTOR

  constructor() {
    super(SUPPORTED_PLATFORMS.GOOGLE)

    this.updateElementStylesForTheme()
    this.checkPageType(this.currentPageType)
  }

  /**
   * 获取单例实例
   */
  static getInstance(): GoogleThemeService {
    if (!GoogleThemeService.instance) {
      GoogleThemeService.instance = new GoogleThemeService()
    }
    return GoogleThemeService.instance
  }

  /**
   * 批量初始化页面样式
   */
  updateElementStylesForTheme(): void {
    // 通用样式初始化
    this.commonPage.init()

    // 动态获取当前页面处理器并初始化
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as GooglePageType]
    if (PageHandlerClass) {
      PageHandlerClass.getInstance().init()
    }
    else if (this.currentPageType === UNDEFINED_PAGE_TYPE) {
      loggerDeduplicator.warn(UNDEFINED_PAGE_TYPE_TIPS)
    }
    else {
      logger.warn(`未找到 ${this.currentPageType} 页面的处理器`)
    }
  }

  platformStyleVar(_theme: ThemeType): string {
    return ``
  }

  getBaseStyles(_theme: ThemeType): string {
    return `
${super.getBaseStyles(_theme)}
`
  }

  getPageStyles(_theme: ThemeType): string {
    return `
html {
  padding: 0 !important;
}

${BODY_SELECTOR} {
  background-color: var(${this.themeVarService.search.default.background}) !important;
}
`
  }

  getMainStyles(theme: ThemeType): string {
    // 1. 通用样式
    let commonStyles = this.commonPage.getMainStyles()
    const notSupportedPages: GooglePageType[] = [
      GooglePageType.NotFound,
    ]

    if (notSupportedPages.includes(this.currentPageType as GooglePageType))
      commonStyles = ''

    // 2. 动态获取当前页面样式
    let pageStyles = ''
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as GooglePageType]
    if (PageHandlerClass) {
      pageStyles = PageHandlerClass.getInstance().getMainStyles(theme)
    }

    return `
  /* ===================== 【页面】通用样式 ===================== */   
  ${commonStyles}
  /* ===================== 【页面】独立样式 ===================== */
  ${pageStyles}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return `
    /* 去掉顶部菜单上方的空白间距 */    
    h1 + div[jscontroller] + div {
      padding-top: 0 !important;
    }
    `
  }

  getFixedStyles(_theme: ThemeType): string {
    return ``
  }
}
